<?php
namespace Common\Model;

class BookArticleModel extends XuecheModel {

    protected $pk = 'article_id';
    protected $tableName = 'book_article';
    protected $fields = array(
            'article_id','book_id','article_name', 'create_time','year','month','day',
            'article_num','piece_num','piece_name','update_time','unique','content'
        );


    public function Validate($data) {
        return TRUE;
    }

    public $_useCache = true;

    //检测章节名称重复
    public function checkArtcileRepeat($unique,$data = ''){
        //$unique = md5($book_id.$article_name);
        if($this->where("`unique` = '{$unique}'")->field($this->pk)->find()){
            return TRUE;
        }
        if(empty($data['article_num']) || empty($data['book_id'])){
            return FALSE;
        }
        $name = $this->where("`book_id`='{$data['book_id']}' AND `article_num`='{$data['article_num']}'")->field('article_name')->select();
        if($name){
            foreach ($name as  $value) {
                # code...
                similar_text($data['article_name'],$value['article_name'],$rep);
                if($rep > 70){
                    return TRUE;
                }
            }
        }
        return FALSE;
    }

    //获取该章节的以及上下章节
    public function nextPrevArticle($article_id,$book_id){
        if($article_id > 0 && $book_id > 0){
            $info = $this->where("`$this->pk` = '{$article_id}' AND book_id='{$book_id}'")->find();
            $next = $this->where("`$this->pk` > '{$article_id}' AND book_id='{$book_id}'")->order("`$this->pk` ASC")->find();
            $prev = $this->where("`$this->pk` < '{$article_id}' AND book_id='{$book_id}'")->order("`$this->pk` DESC")->find();
            $info['next'] = empty($next)?0:$next['article_id'];
            $info['prev'] = empty($prev)?0:$prev['article_id'];
            return $info;
        }
    }

    //获取最新章节
    public function getNewArticle($book_id){
        if($book_id > 0){
            return  $this->where("`book_id` = '{$book_id}'")->order("`update_time` DESC,`{$this->pk}` DESC")->find();
        }
        return FALSE;
    }

    //获取章节名称
    public function getArticleName($article_id){
        if(empty($article_id)){            return FALSE;}
        $name = $this->where("`{$this->pk}` = '{$article_id}'")->field('article_name')->find();
        return $name['article_name'];
    }


    //获取章节列表
    public function getList($where = '',$order = '',$offset = 0,$limit = 18,$total = TRUE){
        $data = parent::getList($where, $order, $offset, $limit,$total);
        if($data['total'] > 0){
            $Books = new BookModel();
            foreach ($data['lists'] as $k=>$v){
                $name = $Books->getList("book_id='{$v['book_id']}'",'',0,1,'cate_id,author_id,book_name',FALSE);
                $data['lists'][$k]['book_name'] = $name['lists'][0]['book_name'];
                $data['lists'][$k]['cate_name'] = $name['lists'][0]['cate_name'];
                $data['lists'][$k]['author_name'] = $name['lists'][0]['author_name'];
            }
        }
        return $data;
    }

    public function getCateUpdate($cate_id = 0){
        if(empty($cate_id)){
            return FALSE;
        }
        $sql = "SELECT distinct(ar.article_id),ar.article_name,bo.book_id,ar.create_time FROM `sdb_book_article` as ar,`sdb_book` as bo where bo.cate_id = '{$cate_id}' AND ar.book_id = bo.book_id ORDER BY ar.create_time DESC limit 30";
        $data = $this->query($sql);
        if(is_array($data)){
            $Books = new BookModel();
            foreach ($data as $k => $v) {
                $name = $Books->getList("book_id='{$v['book_id']}'",'',0,1,'author_id,book_name',FALSE);
                $data[$k]['book_name'] = $name['lists'][0]['book_name'];
                $data[$k]['author_name'] = $name['lists'][0]['author_name'];
            }
        }
        return $data;
    }

    /**
     * 根据主键删除文章标题和文章内容
     * @param type $ids
     */
    public function delArtilce($ids){
        if(empty($ids)){
            return FALSE;
        }
        if(is_numeric($ida)){
            if($this->delete($ids)){
                $Con = new BookContentModel();
                return $Con->delete($ids);
            }
        }
        $ida = explode(',', $ids);
        if(is_array($ida)){
            foreach ($ida as $v){
                if(!is_numeric($v) || empty($v)){
                    return FALSE;
                }
            }
            if($this->delete($ids)){
                $Con = new BookContentModel();
                return $Con->delete($ids);
            }
        }

    }


}
